package com.ruoyi.water.controller;

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.water.domain.PressureSensor;
import com.ruoyi.water.service.IPressureSensorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 压力传感器Controller
 *
 * @author wujiong
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/water/pressuresensor")
public class PressureSensorController extends BaseController
{
    @Autowired
    private IPressureSensorService pressureSensorService;

    /**
     * 查询压力传感器列表
     */
    @PreAuthorize("@ss.hasPermi('water:pressuresensor:list')")
    @GetMapping("/list")
    public TableDataInfo list(PressureSensor pressureSensor)
    {
        startPage();
        // 使用MyBatis-Plus的条件构造器构建查询
        QueryWrapper<PressureSensor> queryWrapper = new QueryWrapper<>(pressureSensor);
        List<PressureSensor> list = pressureSensorService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出压力传感器列表
     */
    @PreAuthorize("@ss.hasPermi('water:pressuresensor:export')")
    @Log(title = "压力传感器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PressureSensor pressureSensor)
    {
        QueryWrapper<PressureSensor> queryWrapper = new QueryWrapper<>(pressureSensor);
        List<PressureSensor> list = pressureSensorService.list(queryWrapper);
        ExcelUtil<PressureSensor> util = new ExcelUtil<PressureSensor>(PressureSensor.class);
        util.exportExcel(response, list, "压力传感器数据");
    }

    /**
     * 获取压力传感器详细信息
     */
    @PreAuthorize("@ss.hasPermi('water:pressuresensor:query')")
    @GetMapping(value = "/{sensorId}")
    public AjaxResult getInfo(@PathVariable("sensorId") Long sensorId)
    {
        // 使用MyBatis-Plus的getById方法
        return success(pressureSensorService.getById(sensorId));
    }

    /**
     * 新增压力传感器
     */
    @PreAuthorize("@ss.hasPermi('water:pressuresensor:add')")
    @Log(title = "压力传感器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PressureSensor pressureSensor)
    {
        // 使用MyBatis-Plus的save方法
        return toAjax(pressureSensorService.save(pressureSensor));
    }

    /**
     * 修改压力传感器
     */
    @PreAuthorize("@ss.hasPermi('water:pressuresensor:edit')")
    @Log(title = "压力传感器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PressureSensor pressureSensor)
    {
        // 使用MyBatis-Plus的updateById方法
        return toAjax(pressureSensorService.updateById(pressureSensor));
    }

    /**
     * 删除压力传感器
     */
    @PreAuthorize("@ss.hasPermi('water:pressuresensor:remove')")
    @Log(title = "压力传感器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sensorIds}")
    public AjaxResult remove(@PathVariable Long[] sensorIds)
    {
        // 使用MyBatis-Plus的removeByIds方法
        return toAjax(pressureSensorService.removeByIds(Arrays.asList(sensorIds)));
    }
}
